package com.lei.day02lx.mvp;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.lei.day02lx.bean.GsonBean;
import com.lei.day02lx.network.response.ResponseTransformer;
import com.lei.day02lx.network.schedulers.BaseSchedulerProvider;

import java.io.Reader;
import java.util.HashMap;
import java.util.List;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * @author Lc
 * @description:
 * @date :2020/11/10 15:27
 */
public class Presenter implements Contract.Presenter {
    private Contract.Model model;

    private Contract.View view;

    private BaseSchedulerProvider schedulerProvider;

    private CompositeDisposable compositeDisposable;//内存泄漏

    public Presenter(Contract.Model model, Contract.View view,
                     BaseSchedulerProvider schedulerProvider) {
        this.model = model;
        this.view = view;
        this.schedulerProvider = schedulerProvider;
        compositeDisposable = new CompositeDisposable();
    }

    public void destroy() {
        compositeDisposable.dispose();//取消掉当前网络请求,防止内存泄漏
    }

    @Override
    public void getCarList() {
        HashMap<String,String> map = new HashMap<>();

        Disposable disposable =
                model.getCartList(map).compose(ResponseTransformer.handleResult()).compose(schedulerProvider.applySchedulers())
                        .subscribe(carBeans -> {
                            //处理数据

                            view.getDataSuccess(carBeans+"");

                        }, throwable -> {
                    //处理异常
                    view.getDataFailed();
                });
        compositeDisposable.add(disposable);//添加进防止内存泄漏
    }
}
